import { Position, Range } from 'vscode-languageserver';

export interface SymbolLocation {
    uri: string;
    range: Range;
}

export class Symbol {
    constructor(
        public readonly name: string,
        public readonly kind: string,
        public readonly range: Range
    ) {}
}

export class SymbolTable {
    private documentSymbols: Map<string, Map<Symbol, SymbolLocation>> = new Map();

	clear(uri: string) {
		this.documentSymbols.delete(uri);
	}

	has(uri: string):boolean {
		return this.documentSymbols.has(uri);
	}
    
    addSymbol(uri: string, symbol: Symbol, location: SymbolLocation) {
        if (!this.documentSymbols.has(uri)) {
            this.documentSymbols.set(uri, new Map());
        }
        this.documentSymbols.get(uri)!.set(symbol, location);
    }
    
    findDefinition(uri: string, position: Position): SymbolLocation | null {
        const symbols = this.documentSymbols.get(uri);
        if (!symbols) return null;
        
        for (const [symbol, location] of symbols) {
            if (this.isPositionInRange(position, symbol.range)) {
                return location;
            }
        }
        return null;
    }
    
    private isPositionInRange(position: Position, range: Range): boolean {
        return (position.line >= range.start.line && 
                position.line <= range.end.line &&
                position.character >= range.start.character && 
                position.character <= range.end.character);
    }
}